<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多iframe通信示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .iframe-container {
            margin: 20px 0;
            border: 1px solid #ddd;
            border-radius: 8px;
            overflow: hidden;
            background: white;
        }
        .iframe-header {
            background: #007bff;
            color: white;
            padding: 10px 15px;
            font-weight: bold;
        }
        .iframe-controls {
            padding: 10px 15px;
            background: #f8f9fa;
            border-bottom: 1px solid #ddd;
        }
        .iframe-controls button {
            margin-right: 10px;
            padding: 5px 15px;
            border: 1px solid #007bff;
            background: white;
            color: #007bff;
            border-radius: 4px;
            cursor: pointer;
        }
        .iframe-controls button:hover {
            background: #007bff;
            color: white;
        }
        .iframe-wrapper {
            height: 300px;
        }
        iframe {
            width: 100%;
            height: 100%;
            border: none;
        }
        .status {
            padding: 10px 15px;
            background: #e9ecef;
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>多iframe通信示例</h1>
        <p>这个示例展示了如何在同一页面中管理多个iframe的导航和URL变化</p>

        <!-- iframe 1 -->
        <div class="iframe-container">
            <div class="iframe-header">iframe-1: Vue应用</div>
            <div class="iframe-controls">
                <button onclick="controllers.iframe1.goBack()">后退</button>
                <button onclick="controllers.iframe1.goForward()">前进</button>
                <span>ID: <span id="iframe1-id">iframe-1</span></span>
            </div>
            <div class="iframe-wrapper">
                <iframe id="iframe1" src="http://localhost:3001"></iframe>
            </div>
            <div class="status">
                当前URL: <span id="iframe1-url">加载中...</span>
            </div>
        </div>

        <!-- iframe 2 -->
        <div class="iframe-container">
            <div class="iframe-header">iframe-2: React应用</div>
            <div class="iframe-controls">
                <button onclick="controllers.iframe2.goBack()">后退</button>
                <button onclick="controllers.iframe2.goForward()">前进</button>
                <span>ID: <span id="iframe2-id">iframe-2</span></span>
            </div>
            <div class="iframe-wrapper">
                <iframe id="iframe2" src="http://localhost:3002"></iframe>
            </div>
            <div class="status">
                当前URL: <span id="iframe2-url">加载中...</span>
            </div>
        </div>

        <!-- iframe 3 (自动ID) -->
        <div class="iframe-container">
            <div class="iframe-header">iframe-3: 自动ID</div>
            <div class="iframe-controls">
                <button onclick="controllers.iframe3.goBack()">后退</button>
                <button onclick="controllers.iframe3.goForward()">前进</button>
                <span>ID: <span id="iframe3-id">自动生成</span></span>
            </div>
            <div class="iframe-wrapper">
                <iframe id="iframe3" src="http://localhost:3003"></iframe>
            </div>
            <div class="status">
                当前URL: <span id="iframe3-url">加载中...</span>
            </div>
        </div>
    </div>

    <script type="module">
        import { IframeController } from '../../dist/index.js';

        // 创建控制器
        const controllers = {
            iframe1: new IframeController('#iframe1', {
                hostOrigin: 'http://localhost:3001',
                iframeId: 'iframe-1'
            }),
            iframe2: new IframeController('#iframe2', {
                hostOrigin: 'http://localhost:3002',
                iframeId: 'iframe-2'
            }),
            iframe3: new IframeController('#iframe3', {
                hostOrigin: 'http://localhost:3003'
            })
        };

        // 监听URL变化
        Object.entries(controllers).forEach(([key, controller]) => {
            const urlElement = document.getElementById(`${key}-url`);
            const idElement = document.getElementById(`${key}-id`);
            
            // 更新ID显示
            idElement.textContent = controller.getIframeId();
            
            // 监听URL变化
            controller.onUrlChange((url, iframeId) => {
                console.log(`${key} URL changed:`, url, 'iframeId:', iframeId);
                urlElement.textContent = url;
            });
        });

        // 暴露到全局作用域用于按钮点击
        window.controllers = controllers;
    </script>
</body>
</html>